﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using ResolutionManager.Properties;
using NET.Tools.OS;

namespace ResolutionManager
{
    public partial class ResolutionManagerContext : ApplicationContext
    {
        private ConfigurationWindow window = null;

        public ResolutionManagerContext()
        {
            InitializeComponents();

            Dictionary<ResolutionType, List<DisplayScreen>> resolutions = IO.GetResolutionList();
            UpdateContextMenu(resolutions[ResolutionType.Resolution2D], resolutions[ResolutionType.Resolution3D]);
        }

        private void ctxIExit_Click(object sender, EventArgs e)
        {
            notifyIcon.Visible = false;
            Application.Exit();
        }

        private void ShowConfigurationWindowEventHandler(object sender, EventArgs e)
        {
            if (window == null)
            {
                window = new ConfigurationWindow();
                window.Show();
                window.FormClosed += new FormClosedEventHandler(window_FormClosed);
                window.UpdateContextMenu += new UpdateContextMenuHandler(window_UpdateContextMenu);
            }
            else
            {
                window.Activate();
            }
        }

        private void window_UpdateContextMenu(object sender, UpdateContextMenuHandlerEventArgs e)
        {
            List<DisplayScreen> list2D = new List<DisplayScreen>(), list3D = new List<DisplayScreen>();

            foreach (Object item in e.Resolution2DList)
            {
                list2D.Add((DisplayScreen)item);
            }

            foreach (Object item in e.Resolution3DList)
            {
                list3D.Add((DisplayScreen)item);
            }

            UpdateContextMenu(list2D, list3D);
        }

        private void UpdateContextMenu(List<DisplayScreen> list2D, List<DisplayScreen> list3D)
        {
            ctxI3D.DropDownItems.Clear();
            foreach (DisplayScreen screen in list3D)
            {
                ToolStripMenuItem item = (ToolStripMenuItem)ctxI3D.DropDownItems.Add(screen.ToString());
                item.Tag = screen;
                item.Click += new EventHandler(ContextResolutionActionHandler);
            }
            if (ctxI3D.DropDownItems.Count <= 0)
            {
                ctxI3D.DropDownItems.Add("Empty").Enabled = false;
            }

            ctxI2D.DropDownItems.Clear();
            foreach (DisplayScreen screen in list2D)
            {
                ToolStripMenuItem item = (ToolStripMenuItem)ctxI2D.DropDownItems.Add(screen.ToString());
                item.Tag = screen;
                item.Click += new EventHandler(ContextResolutionActionHandler);
            }
            if (ctxI2D.DropDownItems.Count <= 0)
            {
                ctxI2D.DropDownItems.Add("Empty").Enabled = false;
            }
        }

        private void ContextResolutionActionHandler(object sender, EventArgs e)
        {
            DisplayScreen screen = (DisplayScreen)((ToolStripItem)sender).Tag;
            screen.UpdateSettingsInRegistry();
        }

        private void window_FormClosed(object sender, FormClosedEventArgs e)
        {
            window = null;
            notifyIcon.ShowBalloonTip(5000);
        }
    }
}
